//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Security.Cryptography;

namespace DNAide.Core
{
	public static partial class StringAide
	{

		#region Left

		/// <summary>
		/// Returns a string containing a specified number of characters from the left side of a string.
		/// </summary>
		/// <param name="value">value</param>
		/// <param name="length">length</param>
		/// <returns>String</returns>
		public static String Left(String value, Int32 length)
		{
			if (string.IsNullOrEmpty(value))
			{
				return string.Empty;
			}
			if (value.Length > length)
			{
				value = value.Substring(0, length);
			}
			return value;
		}

		#endregion


		#region Right

		/// <summary>
		/// Returns a string containing a specified number of characters from the right side of a string.
		/// </summary>
		/// <param name="value">value</param>
		/// <param name="length">length</param>
		/// <returns>String</returns>
		public static String Right(String value, Int32 length)
		{
			if (string.IsNullOrEmpty(value))
			{
				return string.Empty;
			}
			if (value.Length > length)
			{
				value = value.Substring(value.Length - length, length);
			}
			return value;
		}

		#endregion


		#region Mid

		/// <summary>
		/// Returns a string containing a specified number of characters from a string.
		/// </summary>
		/// <param name="value">value</param>
		/// <param name="startIndex">startIndex</param>
		/// <param name="length">length</param>
		/// <returns>String</returns>
		public static String Mid(String value, Int32 startIndex, Int32 length)
		{
			if (string.IsNullOrEmpty(value))
			{
				return string.Empty;
			}
			value = value.Substring(startIndex, length);
			return value;
		}


		/// <summary>
		/// Returns a string containing a specified number of characters from a string.
		/// </summary>
		/// <param name="value">value</param>
		/// <param name="startIndex">startIndex</param>
		/// <returns>String</returns>
		public static String Mid(String value, Int32 startIndex)
		{
			if (string.IsNullOrEmpty(value))
			{
				return string.Empty;
			}
			value = value.Substring(startIndex);
			return value;
		}

		#endregion


		#region Truncate


		/// <summary>
		/// Returns a string trunated and appended with "..." closing characters
		/// </summary>
		/// <example>
		///		StringAide.Truncat("This is a test", 4)
		/// 
		///		would return "This..."
		/// </example>
		/// <param name="value">value</param>
		/// <param name="truncateAt">truncateAt</param>
		/// <returns>String</returns>
		public static String Truncate(String value, Int32 truncateAt)
		{
			return Truncate(value, truncateAt, "...");
		}

		/// <summary>
		/// Returns a string trunated and appended with closing characters
		/// </summary>
		/// <example>
		///		StringAide.Truncat("This is a test", 4, " ...more >")
		/// 
		///		would return "This ...more >"
		/// </example>
		/// <param name="value">value</param>
		/// <param name="truncateAt">truncateAt</param>
		/// <param name="closingCharacters">closingCharacters</param>
		/// <returns>String</returns>
		public static String Truncate(String value, Int32 truncateAt, String closingCharacters)
		{
			if (string.IsNullOrEmpty(value))
			{
				return string.Empty;
			}

			if (closingCharacters == null)
			{
				closingCharacters = string.Empty;
			}

			string retval = value;
			if (retval.Length > truncateAt && truncateAt > 0)
			{
				retval = retval.Substring(0, truncateAt);
				// Check if the character right after the truncate point was a space
				// if not, we are in the middle of a word and need to cut out the rest of it
				if (value.Substring(retval.Length, 1) != " ")
				{
					int lastSpace = retval.LastIndexOf(" ");

					// if we found a space then, cut back to that space
					if (lastSpace != -1)
					{
						retval = retval.Substring(0, lastSpace);
					}
					// Finally, add the closing characters
					retval = string.Concat(retval, closingCharacters);   
				}
			}

			return retval;
		}

		#endregion
	}
}
